/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.FrameControlDanhMuc;

import GUI.FrameControlKhoHang.jframePhieuNhapSach;
import TOOL.Ulti;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jframeThemSach extends javax.swing.JFrame 
{
    private JIFSach parent;
    /**
     * Creates new form jframeThemSach
     */
    public jframeThemSach(JIFSach par) 
    {
        parent = par;
        initComponents();
        
        jTextFieldSoLuongTon.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((c >= '0') && (c <= '9') ||
                    (c == KeyEvent.VK_BACK_SPACE) ||
                    (c == KeyEvent.VK_DELETE))) {
                getToolkit().beep();
                e.consume();
          }
        }
        });
        
        this.setLocation(400, 200);
        ResetForm();
        //Them the loai va tac gia vao cac combobox
        new BUS.Sach().LoadAuthor((Object)jComboBoxTacGia);
        new BUS.Sach().LoadCategory((Object)jComboBoxTheLoai);
    }
            
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldMaSach = new javax.swing.JTextField();
        jTextFieldTenSach = new javax.swing.JTextField();
        jComboBoxTacGia = new javax.swing.JComboBox();
        jComboBoxTheLoai = new javax.swing.JComboBox();
        jButtonLuuThoat = new javax.swing.JButton();
        jButtonLuu = new javax.swing.JButton();
        jButtonThoat = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldSoLuongTon = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabelTieuDe = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm Sách");

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel7.setText("Mã Sách:");

        jLabel1.setText("Tên Sách:");

        jLabel2.setText("Tác giả:");

        jLabel3.setText("Thể Loại:");

        jTextFieldMaSach.setEnabled(false);

        jTextFieldTenSach.setText("Sách ABC");
        jTextFieldTenSach.setSelectionStart(0);

        jComboBoxTacGia.setEditable(true);

        jComboBoxTheLoai.setEditable(true);

        jButtonLuuThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/save1.png"))); // NOI18N
        jButtonLuuThoat.setText("Lưu và Thoát");
        jButtonLuuThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLuuThoatActionPerformed(evt);
            }
        });

        jButtonLuu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/save.png"))); // NOI18N
        jButtonLuu.setText("Lưu");
        jButtonLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLuuActionPerformed(evt);
            }
        });

        jButtonThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close2.png"))); // NOI18N
        jButtonThoat.setText("Thoát");
        jButtonThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThoatActionPerformed(evt);
            }
        });

        jLabel4.setText("Số Lượng Tồn");

        jTextFieldSoLuongTon.setText("000");
        jTextFieldSoLuongTon.setToolTipText("");
        jTextFieldSoLuongTon.setSelectionStart(0);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldTenSach)
                            .addComponent(jComboBoxTacGia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBoxTheLoai, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextFieldMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jTextFieldSoLuongTon)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonLuuThoat)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonThoat, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextFieldMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addComponent(jTextFieldTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxTacGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldSoLuongTon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 85, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonLuu)
                    .addComponent(jButtonLuuThoat)
                    .addComponent(jButtonThoat))
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(49, 122, 233));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabelTieuDe.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabelTieuDe.setForeground(new java.awt.Color(255, 255, 255));
        jLabelTieuDe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        jLabelTieuDe.setText("Thêm Sách mới");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTieuDe)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTieuDe)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonThoatActionPerformed

    private void jButtonLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLuuActionPerformed
          if(!Validate())
        {
            JOptionPane.showMessageDialog(this, "Chưa nhập đầy đủ thông tin", "Lỗi", JOptionPane.ERROR_MESSAGE);
            return ;
        }
        if(!Insert())
            JOptionPane.showMessageDialog(this, "Lỗi thêm vào CSDL", "Lỗi", JOptionPane.ERROR_MESSAGE);
        else
        {
            if(parent != null)
                parent.LoadData();
            ResetForm();
            
        }
    }//GEN-LAST:event_jButtonLuuActionPerformed

    private void jButtonLuuThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLuuThoatActionPerformed
        // TODO add your handling code here:
        if(!Validate())
        {
            JOptionPane.showMessageDialog(this, "Chưa nhập đầy đủ thông tin", "Lỗi", JOptionPane.ERROR_MESSAGE);
            return ;
        }
        if(!Insert())
            JOptionPane.showMessageDialog(this, "Lỗi thêm vào CSDL", "Lỗi", JOptionPane.ERROR_MESSAGE);
        else
        {
            if(parent != null)
                parent.LoadData();
            dispose();
        }
    }//GEN-LAST:event_jButtonLuuThoatActionPerformed
    
    private void ResetForm()
    {
        jTextFieldMaSach.setText(new BUS.Sach().GenerateID());
        jTextFieldTenSach.setText("Sách ABC");
        jTextFieldTenSach.selectAll();
        jTextFieldSoLuongTon.setText("000");
        jTextFieldSoLuongTon.selectAll();
    }
    
    //Them vào CSDL
    private boolean Insert()
    {
        return new DTO.Sach(Ulti.S(jTextFieldMaSach),//Max sach
         Ulti.S(jTextFieldTenSach),//Ten Sach
        ((String)jComboBoxTheLoai.getSelectedItem()).trim(),//The Loai
        ((String)jComboBoxTacGia.getSelectedItem()).trim(),//Tac gia
        Ulti.S(jTextFieldSoLuongTon)).Insert();//So luong Ton
    }
    
    private boolean Validate()
    {
        boolean b ;
        try
        {
            b = Integer.parseInt(Ulti.S(jTextFieldSoLuongTon)) >= 0;
        }
        catch(Exception ex)
        {
            return false;
        }
        return ! Ulti.S(jTextFieldTenSach).isEmpty() ;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLuu;
    private javax.swing.JButton jButtonLuuThoat;
    private javax.swing.JButton jButtonThoat;
    private javax.swing.JComboBox jComboBoxTacGia;
    private javax.swing.JComboBox jComboBoxTheLoai;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabelTieuDe;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextFieldMaSach;
    private javax.swing.JTextField jTextFieldSoLuongTon;
    private javax.swing.JTextField jTextFieldTenSach;
    // End of variables declaration//GEN-END:variables
}
